<template>
    <div class="index-lists">
        <div v-show="false">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="资源标题" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" />
                </el-form-item>
                <el-form-item label="link类型" prop="linkType" v-show="false">
                    <el-input class="w-[100px]" v-model="queryParams.linkType" />
                </el-form-item>
                <el-form-item label="linkId" prop="linkId" v-show="false">
                    <el-input class="w-[100px]" v-model="queryParams.linkId" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="">
            <div class="flex flex-row items-center">
                <!-- <el-button v-perms="['wisdbook:resource:add']" type="primary" size="small" @click="handleAdd('1')">富文本</el-button> -->
                <el-button v-perms="['wisdbook:resource:add']" type="primary" size="small" @click="handleAdd('2')">+文档</el-button>
                <el-button v-perms="['wisdbook:resource:add']" type="primary" size="small" @click="handleAdd('3')">+图片</el-button>
                <el-button v-perms="['wisdbook:resource:add']" type="primary" size="small" @click="handleAdd('4')">+视频</el-button>
                <el-button v-perms="['wisdbook:resource:add']" type="primary" size="small" @click="handleAdd('5')">+音频</el-button>
                <!-- <el-button v-perms="['wisdbook:resource:add']" type="primary" size="small" @click="handleAdd('6')">+题目ID</el-button> -->
                <!-- <el-button v-perms="['wisdbook:resource:add']" type="warning" size="small" @click="handleAdd('7')">+原书答案</el-button> -->
            </div>
            
            <el-table class="mt-2  min-h-[400px]" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="资源类型" prop="type" width="100" >
                    <template #default="scope">
                        <div style="display: flex;">
                        <span style="margin-left: 10px">{{ resTypes[scope.row.type] }}</span>
                        </div>
                    </template>

                </el-table-column>
                <el-table-column label="资源标题" prop="title" width="200" />
                <el-table-column label="资源内容" prop="content" min-width="100" />
                <el-table-column label="操作" width="120">
                    <template #default="{ row }">
                        <el-button v-perms="['wisdbook:resource:edit']" type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <el-button v-perms="['wisdbook:resource:del']" type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="info text-[#999]">LinkType:{{ queryParams.linkType }} LinkID:{{ queryParams.linkId }} </div>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </div>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="resource">
import { resourceDelete, resourceLists } from '@/api/wisdbook/resource'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const resTypes=ref(['','富文本','文档','图片','视频','音频','题目','原书答案'])
const props = defineProps({
    linkType: {
        type: String,
        default: "",
    },
    linkId: {
        type: String,
        default: "",
    }
});

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)


const queryParams = reactive({
    type: '',
    title: '',
    content: '',
    linkType: props.linkType,
    linkId: props.linkId,
})

watch(
    props,
    (old, newProps) => {
        console.log("2222222")
        queryParams.linkType=newProps.linkType,
        queryParams.linkId=newProps.linkId
        getLists();
    }
)


const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: resourceLists,
    params: queryParams
})



const handleAdd = async (type:string) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
    editRef.value?.setFormData({
        type:type,
        linkType: props.linkType,
        linkId: props.linkId,
    })
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await resourceDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}


getLists()

</script>
